
package com.example.medical_delivery_system.api;

import com.example.medical_delivery_system.common.Constants;
import io.swagger.annotations.*;
import com.example.medical_delivery_system.api.param.LoginParam;
import com.example.medical_delivery_system.api.param.RegisterParam;
import com.example.medical_delivery_system.api.param.UpdatePasswordParam;
import com.example.medical_delivery_system.api.param.UpdateNameParam;
import com.example.medical_delivery_system.common.ServiceResultEnum;
import com.example.medical_delivery_system.config.annotation.TokenToUser;
//import com.example.medical_delivery_system.api.vo.UserVO;
import com.example.medical_delivery_system.entity.User;
import com.example.medical_delivery_system.entity.Token;
import com.example.medical_delivery_system.service.UserService;
import com.example.medical_delivery_system.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@Api(value = "v1", tags = "2.医疗配送系统用户操作相关接口")
@RequestMapping("/api/v1")
public class PersonalAPI {

    @Resource
    private UserService userService;

    private static final Logger logger = LoggerFactory.getLogger(PersonalAPI.class);

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result<String> login(@RequestBody @Valid LoginParam userLoginParam) {
        String loginResult = userService.login(userLoginParam.getLoginName(), userLoginParam.getUserPasswd());
        logger.info("login api,nickName={},loginResult={}", userLoginParam.getLoginName(), loginResult);

        //登录成功
        if (!StringUtils.isEmpty(loginResult) && loginResult.length() == Constants.TOKEN_LENGTH) {
            Result result = ResultGenerator.genSuccessResult();
            result.setData(loginResult);
            return result;
        }
        //登录失败
        return ResultGenerator.genFailResult(loginResult);
    }


    @PostMapping("/logout")
    @ApiOperation(value = "登出接口", notes = "清除token")
    public Result<String> logout(@TokenToUser User loginUser) {
        Boolean logoutResult = userService.logout(loginUser.getUId());

        logger.info("logout api,loginUser={}", loginUser.getUId());

        //登出成功
        if (logoutResult) {
            return ResultGenerator.genSuccessResult();
        }
        //登出失败
        return ResultGenerator.genFailResult("logout error");
    }


    @PostMapping("/register")
    @ApiOperation(value = "用户注册", notes = "")
    public Result register(@RequestBody @Valid RegisterParam userRegisterParam) {
//        if (!NumberUtil.isPhone(userRegisterParam.getLoginName())){
//            return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_NAME_IS_NOT_PHONE.getResult());
//        }
        String registerResult = userService.register(userRegisterParam.getRegisterName(), userRegisterParam.getRegisterPasswd());

        logger.info("register api,loginName={},loginResult={}", userRegisterParam.getRegisterName(), registerResult);

        //注册成功
        if (ServiceResultEnum.SUCCESS.getResult().equals(registerResult)) {
            return ResultGenerator.genSuccessResult();
        }
        //注册失败
        return ResultGenerator.genFailResult(registerResult);
    }

    @RequestMapping(value = "/updatePassword", method = RequestMethod.PUT)
    public Result passwordUpdate(@RequestBody @Valid UpdatePasswordParam updatePasswordParam, @TokenToUser Token user) {
        logger.info("user:{}", user.toString());
        if (userService.updatePassword(user.getUId(), updatePasswordParam.getOriginalPasswd(), updatePasswordParam.getNewPasswd())) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(ServiceResultEnum.DB_ERROR.getResult());
        }
    }

    @RequestMapping(value = "/updateName", method = RequestMethod.PUT)
    public Result nameUpdate(@RequestBody @Valid UpdateNameParam updateNameParam, @TokenToUser Token user) {
        logger.info("user:{}", user.toString());
        if (userService.updateName(user.getUId(), updateNameParam.getNewName())) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(ServiceResultEnum.DB_ERROR.getResult());
        }
    }

    
}
